import {SpeedType} from 'JoystickCommon'
import JoystickCommon from 'JoystickCommon'

var GameData = require('../../module/GameData')();


var Check_Collision = true;
/*是否开启碰撞检测*/
cc.Class({
    extends: cc.Component,
    properties: {
        // from joystick
        moveDir: null,

        //     {
        //     default: cc.v2(0, 1),
        //     displayName: 'Move Dir',
        //     tooltip: '移动方向',
        // },
        _speedType: {
            default: SpeedType.STOP,
            displayName: 'Speed Type',
            type: SpeedType,
            tooltip: '速度级别'
        },
        // from self
        _moveSpeed: {
            default: 2,
            displayName: 'Move Speed',
            tooltip: '移动速度'
        },
        stopSpeed: {
            default: 0,
            type: cc.Integer,
            tooltip: '停止时速度'
        },
        normalSpeed: {
            default: 2,
            type: cc.Integer,
            tooltip: '正常速度'
        },
        fastSpeed: {
            default: 3,
            type: cc.Integer,
            tooltip: '最快速度'
        },
        row: cc.Label,
        col: cc.Label,
    },
    onEnable() {

    },
    // methods
    move() {
        if (!this.moveDir) {
            return;
        }
        var _angle = cc.misc.radiansToDegrees(
            Math.atan2(this.moveDir.y, this.moveDir.x)
        );
        this.node.angle = _angle - 90;
        if (!Check_Collision) {
            this.playerMove();
        } else {
            var addPos = this.moveDir.mul(this._moveSpeed);
            let bool =this.get_can_move(addPos);
            if(!bool){
                this.get_can_pos();
            }

        }
    },
    playerMove() {
        let newPos = this.node.position.add(this.moveDir.mul(this._moveSpeed));
        this.node.setPosition(newPos);
    },
    get_can_move(pos) {
        let newPos = this.node.position.add(cc.v2(pos.x, pos.y));
        let bool = this.check_is_move(newPos);
        if (bool) {
            return true;
        }
    },
    /*判断斜方向是否能行走*/
    get_can_pos() {
        var addPos = this.moveDir.mul(this._moveSpeed);
        let _x = 0;
        let _y = 0;
        if (addPos.x > 0 && addPos.y > 0) {/*向第一象限移动*/
            if (Math.abs(addPos.x) > Math.abs(addPos.y)) {
                _x= addPos.mag();
                let bool = this.get_can_move(cc.v2(_x, _y));
                if (!bool) {
                    _x=0;
                    _y = addPos.mag();
                    this.get_can_move(cc.v2(_x, _y));
                }
            } else if(Math.abs(addPos.x) <= Math.abs(addPos.y)){
                _y = addPos.mag();
                let bool = this.get_can_move(cc.v2(_x, _y));
                if (!bool) {
                    _y=0;
                    _x= addPos.mag();
                    this.get_can_move(cc.v2(_x, _y));
                }
            }
        } else if (addPos.x < 0 && addPos.y > 0) {/*向第二象限移动*/
            if (Math.abs(addPos.x) > Math.abs(addPos.y)) {
                _x= -addPos.mag();
                let bool = this.get_can_move(cc.v2(_x, _y));
                if (!bool) {
                    _y = addPos.mag();
                    _x=0;
                    this.get_can_move(cc.v2(_x, _y));
                }
            }
            if(Math.abs(addPos.x) <= Math.abs(addPos.y)){
                _y = addPos.mag();
                _x=0;
                let bool = this.get_can_move(cc.v2(_x, _y));
                if (!bool) {
                    _x= -addPos.mag();
                    _y=0;
                    this.get_can_move(cc.v2(_x, _y));
                }
            }
        } else if (addPos.x < 0 && addPos.y < 0) {/*向第三象限移动*/

            if (Math.abs(addPos.x) > Math.abs(addPos.y)) {
                _x= -addPos.mag();
                let bool = this.get_can_move(cc.v2(_x, _y));
                if (!bool) {
                    _y = -addPos.mag();
                    _x=0;
                    this.get_can_move(cc.v2(_x, _y));
                }
            }
            if(Math.abs(addPos.x) <= Math.abs(addPos.y)){
                _y = -addPos.mag();
                _x=0;
                let bool = this.get_can_move(cc.v2(_x, _y));
                if (!bool) {
                    _x= -addPos.mag();
                    _y=0;
                    this.get_can_move(cc.v2(_x, _y));
                }
            }

        } else if (addPos.x > 0 && addPos.y < 0) {/*向第四象限移动*/
            if (Math.abs(addPos.x) > Math.abs(addPos.y)) {
                _x= addPos.mag();
                let bool = this.get_can_move(cc.v2(_x, _y));
                if (!bool) {
                    _y = -addPos.mag();
                    _x=0;
                    this.get_can_move(cc.v2(_x, _y));
                }
            }
            if(Math.abs(addPos.x) <= Math.abs(addPos.y)){
                _y = -addPos.mag();
                _x=0;
                let bool = this.get_can_move(cc.v2(_x, _y));
                if (!bool) {
                    _x= addPos.mag();
                    _y=0;
                    this.get_can_move(cc.v2(_x, _y));
                }
            }
        }

        // this.node.setPosition(newPos);
    },
    check_is_move(newPos) {
        let row = Math.round((newPos.x - GameData.All_block[0].x) / 45);
        let col = Math.round((newPos.y - GameData.All_block[0].y) / 45);
        let _num = row * 23 + col;
        let _obj = GameData.All_block[_num];
        if (!_obj.obj) { /*判断前进方向是否有障碍物 如果有则进行逻辑处理，如果没有不做任何逻辑处理*/
            this.node.setPosition(newPos);
            return true;
        } else {
            this.row.string = row + "";
            this.col.string = col + "";
        }
    },
    checkCollision() {

    },
    update(dt) {
        if (!JoystickCommon.JoystickWork) {
            this._moveSpeed = SpeedType.STOP;
            return;
        }
        switch (this._speedType) {
            case SpeedType.STOP:
                this._moveSpeed = this.stopSpeed;
                break;
            case SpeedType.NORMAL:
                this._moveSpeed = this.normalSpeed;
                break;
            case SpeedType.FAST:
                this._moveSpeed = this.fastSpeed;
                break;
            default:
                break;
        }
        this.move();

        // console.log(this.moveDir)
        // console.log(this._moveSpeed)
    },


});
